import dayjs from 'dayjs';
import type { TableListItem } from '@/api/project/project.d';
import type { DataEnum } from '@/api/project/project.d';
import type { TableColumnData } from '@arco-design/web-vue';
import { exportTransfer } from "@/api/projectDetail/projectDetail";
import { Message } from "@arco-design/web-vue";

export default function useUtils(){

    const PubMethodEnum: DataEnum = {
        1: '开放式',
        2: '承包式'
    };
    
    const AssignMethodEnum: DataEnum = {
        1: '抢单式',
        2: '交叉式'
    };
    const AnnoResultTypeEnum: DataEnum = {
        1: '序列标注结果',
        2: '影像标注结果'
    };

    // 根据表格数据返回相应文本值
    const getTableTextValue = (row: TableListItem, list: {
        name: string;
        value: string;
    }[], field: keyof TableColumnData) => list?.filter(item => row[field] === item.value)[0]?.name;

    // 根据表格数据的时间戳返回字符串日期格式
    const getTableDateValue = (row: TableListItem, field: keyof TableColumnData, type: number = 0) => type === 0 ? dayjs(row[field]).format('YYYY-MM-DD') : dayjs(row[field]).format('YYYY-MM-DD hh:mm:ss');
    //预处理导出结果
    const handleExport = async (taskId: any) => {
        const blobData:any = await exportTransfer({taskId}, {responseType: 'blob',})
        const blob = new Blob([blobData], { type: "application/vnd.ms-excel;charset=utf-8" })
        const fileName = '导出结果.xlsx';
        const a = document.createElement("a");
        if (!('download' in a)) {
            Message.warning('该浏览器下载不支持')
            return
        }
        const href = window.URL.createObjectURL(blob);
        a.href = href;
        a.download = fileName;
        (document.body || document.documentElement).appendChild(a);
        a.click();
        (document.body || document.documentElement).removeChild(a);
        window.URL.revokeObjectURL(href);
    }

    return {
        PubMethodEnum,
        AssignMethodEnum,
        AnnoResultTypeEnum,
        getTableTextValue,
        getTableDateValue,
        handleExport,
    }
}